
/* 
 * This file is part of FreeBMS - an open source battery management system
 *
 * Cell mappings header
 * 
 * NOTE: This code is designed to be compiled using SOURCEBOOST (http://www.sourceboost.com)
 */
 
/* This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef _MAPPINGS_H_
#define _MAPPINGS_H_

extern unsigned char localportb;
extern unsigned char localportc;
extern unsigned char localportd;
extern unsigned char localporte;

void set_cell(unsigned char cell, unsigned char state) {

	// Set initial status to prevent relays turning on
	localportb = 0x00;
	localportc = 0x00;
	localportd = 0x00;
	localporte = 0x00;

	// Determine cell
	switch(cell) {
		case 11:
			localportd.RD2 = state;
			break;
		case 12:
			localportd.RD3 = state;
			break;
		case 13:
			localportc.RC4 = state;
			break;
		case 14:
			localportc.RC5 = state;
			break;
		case 15:
			localportd.RD4 = state;
			break;
		case 16:
			localportd.RD1 = state;
			break;
		case 17:
			localportd.RD0 = state;
			break;
		case 18:
			localportc.RC3 = state;
			break;
		case 19:
			localportc.RC2 = state;
			break;
		case 20:
			localportc.RC1 = state;
			break;
		case 10:
			localportc.RC0 = state;
			break;
		case 9:
			localporte.RE2 = state;
			break;
		case 8:
			localporte.RE1 = state;
			break;
		case 7:
			localporte.RE0 = state;
			break;
		case 6:
			localportb.RB4 = state;
			break;
		case 5:
			localportd.RD5 = state;
			break;
		case 4:
			localportd.RD6 = state;
			break;
		case 3:
			localportd.RD7 = state;
			break;
		case 2:
			localportb.RB0 = state;
			break;
		case 1:
			localportb.RB1 = state;
			break;
		default:
			break;
	}
	
	// Update real copies from shadows
	portb = localportb;
	portc = localportc;
	portd = localportd;
	porte = localporte;
}

/*
volatile bit cell1 @ PORTD . RD3;
1: RD2
2: RD3
3: RC4
4: RC5
5: RD4
6: RD1
7: RD0
8: RC3
9: RC2
10: RC1
11: RC0
12: RE2
13: RE1
14: RE0
15: RB4
16: RD5
17: RD6
18: RD7
19: RB0
20: RB1

LED1: RA2
LED2: RA1

2.85v/3.3v: RB5
Vref+: RA3
Voltage Sense: RA0
*/



#endif // _MAPPINGS_H_
